package com.nineclock.attendance.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@Component
public class NcFeignInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        //动态获取原来请求的token
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if(requestAttributes != null){
            ServletRequestAttributes attributes = (ServletRequestAttributes) requestAttributes;
            HttpServletRequest request = attributes.getRequest();
            String authorization = request.getHeader("Authorization");
            //在远程调用 微服务的请求中携带token
            template.header("Authorization", authorization);
        }


    }
}
